#!/usr/bin/ruby
$:.unshift File.join(File.dirname(__FILE__))
require 'AWS/EC2'
require 'basic_configuration'
require 'aws_context'
require 'pp'

# Display rules for a given security group.

if ARGV[0] == '-?' || ARGV.size != 1 then
   puts "usage: list-group group"
   exit 1
end
group=ARGV[0]

Signal.trap("INT") do
  exit 2
end

config = BasicConfiguration.new
ec2 = AwsContext.instance.ec2(*config.keys)
begin
  groups = ec2.describe_security_groups(group)
  #pp groups
  groups.each do |group|
     puts "#{group[:name]} -- #{group[:description]}"
     grants = group[:grants]
     grants.each do |grant|
       puts "  #{grant[:protocol]} #{grant[:from_port]}-#{grant[:to_port]} #{grant[:to_ort]} #{grant[:ip_range]}"
     end
  end
rescue
  puts "error #{$!} #{group}"
  exit 1
end
